<template>
  <a-row id="globalHeader" style="margin-bottom: 16px" align="center" :wrap="false">
    <a-col flex="auto">
      <a-menu mode="horizontal" :selected-keys="selectedKey" @menu-item-click="handleMenuClick">
        <a-menu-item key="0" :style="{ padding: 0, marginRight: '38px' }" disabled>
          <div class="title">
            <img class="logo" src="../assets/logo.png" alt="logo" />
            <div class="titleName">QOJ</div>
          </div>
        </a-menu-item>
        <a-menu-item class="menuItem" v-for="item in visibleRoutes" :key="item.path"
          >{{ item.name }}
        </a-menu-item>
      </a-menu>
    </a-col>
    <a-col flex="100px">
      <div class="user-login-status">
        {{ loginUserStore.loginUser.userAccount }}
        <!--        <div v-if="loginUserStore.loginUser.id">-->
        <!--          {{ loginUserStore.loginUser.username }}-->
        <!--        </div>-->
        <!--        <div v-else>-->
        <!--          <a-button type="primary" href="/user/login">登录</a-button>-->
        <!--        </div>-->
      </div>
    </a-col>
  </a-row>
</template>

<script setup lang="ts">
import { routes } from '../router/routes'
import { useRouter } from 'vue-router'
import { ref } from 'vue'
import { useLoginUserStore } from '@/stores/loginUserStore.ts'
import checkAccess from '@/access/checkAccess.ts'

const loginUserStore = useLoginUserStore()

const router = useRouter()

const visibleRoutes = routes.filter((item, index) => {
  if (item.meta?.hideinmenu) {
    return false
  }
  //根据权限过滤菜单
  if (!checkAccess(loginUserStore.loginUser, item.meta?.access as any)) {
    return false
  }
  return true
})

// 当前选中的菜单, 默认主页
const selectedKey = ref(['/'])

// 监听路由变化
router.afterEach((to, from, failure) => {
  selectedKey.value = [to.path]
})

const handleMenuClick = (key: string) => {
  router.push({
    path: key,
  })
}
</script>

<style scoped>
#globalHeader {
  display: flex;
  width: 100%;
  padding: 0;
  margin: 0;
}

.logo {
  width: 48px;
}

.title {
  display: flex;
  align-items: center;
  gap: 16px;
}

.titleName {
  font-size: 24px;
  color: #222222;
}

.menuItem {
  font-size: 20px;
}
</style>
